package random.fox.p2p.ui;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.Toast;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import flow.Flow;
import random.fox.p2p.App;
import random.fox.p2p.Paths;
import random.fox.p2p.R;
import random.fox.p2p.event.ActionBarEvent;
import random.fox.p2p.event.ProgressEvent;
import random.fox.p2p.model.IndexModule;
import random.fox.p2p.pathview.HandlesBack;
import random.fox.p2p.presenter.IndexPresenter;
import random.fox.p2p.view.IIndexView;

/**
 * Created by wq on 15/3/15.
 */
public class IndexView extends LinearLayout implements IIndexView ,HandlesBack {

    @InjectView(R.id.toolbar)Toolbar toolbar;
    @InjectView(R.id.index_recycle_view)RecyclerView recyclerView;
    @Inject IndexPresenter presenter;

    public IndexView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ButterKnife.inject(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        ((App) getContext().getApplicationContext()).createScopedGraph(new IndexModule(this)).inject(this);
        toolbar.setTitle("今日*发现");
        EventBus.getDefault().post(new ActionBarEvent(toolbar, -1, false));
        presenter.loadData(recyclerView);

    }

    @OnClick(R.id.edit) void editClicked(){
        Flow.get(getContext()).set(new Paths.SendMessagePath());
    }

    @Override
    public void showProgress() {
        EventBus.getDefault().post(new ProgressEvent(true));
    }

    @Override
    public void hideProgress() {
        EventBus.getDefault().post(new ProgressEvent(false));
    }


    @Override
    public void showMessage(String message) {
        Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onBackPressed() {
        presenter.releaseData(); //退出当前界面了
        return false;
    }
}
